<?php

	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

	require("frmCookie.php");
	
	$iOpcion = $_GET[iOpcion];
	$iMensaje = $_GET[iMensaje];
	$idProducto = $_REQUEST['producto'];
	
	switch ($iMensaje)
	{
		case 1:
		{
			$mensaje = "El movimiento se registro con exito.";
			break;
		}
	}
?>
<html>
	<head>
    	<?php
		require("frmHeader.php");
		?>
		<script language="javascript" src="scripts/ajax.js"></script>
		<script language="javascript" src="scripts/validar.js"></script>
		<script language="javascript">
		function valida()
		{
			frm.txtDeposito.value = frm.cboAlmacen.options[frm.cboAlmacen.selectedIndex].text;
			return true;
		}
		</script>
	</head>
	<body>
    	<table cellpadding="0" cellspacing="0" align="center" class="tablaPrincipal">
        	<tr>
            	<td align="center">
                	<?php
					require("frmSession.php"); 
					
					//Redirecciona a SINPERMISOS.PHP en caso de no tener permisos
					switch ($iOpcion)
					{
						case 1:
						case 2:
						{
							
							//if (!(in_array(13001, $_SESSION[permisos])))
							if (!(in_array(44000, $_SESSION[permisos])))
							{
							?>
							  <script language="JavaScript" type="text/javascript">
							  <!--
							  window.location.href = "sinPermisos.php"
							  //-->
							  </script>
							<?php
							}
							$titulo = "Existencias de Stock";
							break;
						}
					}
					
					?>
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<?php
					require("frmTitulo.php");
					?>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="menuPrincipal">
                    	<tr>
                        	<td>
								<?php
                                require("frmMenu.php");
                                ?>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="tituloPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $titulo; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="estadoPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $mensaje; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="contenedorPrincipal">
                    	<tr>
                        	<td align="">
                        		<?php
                				switch($iOpcion)
                				{
                					case 1:
                					{
        						?>
        						<form name="frm" action="existencias.php?iOpcion=2" method="post" onSubmit="return valida();">
        							<table width="400">
        						<?php
                						break;
                					}
                					case 2:
                					{
            					?>
            						<table>
            					<?php	
                						break;
                					}
                				}
        						?>
		                				<?php
		                				switch($iOpcion)
		                				{
		                					case 1:
		                					{
                						?>
                						<tr>
		                					<td width="110">*Deposito: </td>
		                					<td>
		                						<select name="cboAlmacen" style="width:100%;">
			                                       <?php
			    							        $mySQL = "SELECT * FROM almacenes WHERE idSucursal=" . $_SESSION['idSucursal'] . " ORDER BY nombre";
											                    							
			    							        $result = readSQL($mySQL);
			    							
					  				                foreach($result as $value)
					  				                {
				  					                	echo "<option value='" . $value[0] . "'>" . $value['nombre'] . "</option>";
					  				                }
			    							       ?>
			                                      </select>
		                					</td>
		                				</tr>
		                				<tr>
		                					<td colspan="2" align="center"><input type="submit" name="cmdEnviar" value="Continuar"></td>
		                				</tr>
		                				<input type="hidden" name="txtDeposito" value=""/>
                						<?php
		                						break;
		                					}
		                					case 2:
		                					{
		                						if(stristr($_POST[txtDeposito], "BULK") || stristr($_POST[txtDeposito], "BATCH") || stristr($_POST[txtDeposito], "EXPRESS") || stristr($_GET[deposito], "BULK") || stristr($_GET[deposito], "BATCH") || stristr($_GET[deposito], "EXPRESS") || stristr($_GET[deposito], "Transito") || stristr($_POST[txtDeposito], "Transito"))
		                						{
            							?>
		                							<tr>
	            										<td colspan="6">Deposito: <b><?php echo $_POST[txtDeposito] . $_GET[deposito]; ?></b></td>
	            									</tr>
	            									<tr>
	            										<td>&nbsp;</td>
	            									</tr>
	            									<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order1;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Modelo</a></th>
	            									<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order1;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Imei</a></th>
	            									<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order1;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Cliente</a></th>
		            								<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order2;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Nro.Remito Emisor</a></th>
                                                    <th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order2;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Nro.Remito Receptor</a></th>
		            								<?php
			            							//$mySQL = "SELECT O.nroRemitoEmisor, O.nroRemitoReceptor, O.imei, C.nombre, M.nombreComercial, MC.nombre AS MARCA FROM ordenes O, clientes C, modelos M, marcas MC WHERE O.idModelo = M.idModelo AND MC.idMarca = M.idMarca AND C.idCliente = O.idCliente AND O.idAlmacen = " . $_POST[cboAlmacen] . $_GET[almacen] . " and O.idEstado in (select idEstado from estados E where entregado = 0)" . $orderBy;
													//Pregunta a Fabi
													
													$mySQL = "select ma.nombre as MARCA, mo.nombreComercial, o.imei, c.nombre, o.nroRemitoEmisor, o.nroRemitoReceptor from ordenes o join usuarios u on o.idUsuario = u.idUsuario join almacenes a on o.idAlmacen = a.idAlmacen join modelos mo on o.idModelo = mo.idModelo join clientes c on o.idCliente = c.idCliente join marcas ma on mo.idMarca = ma.idMarca where o.idAlmacen = " . $_POST[cboAlmacen] . $_GET[almacen] . " and ((o.idestado in (select idestado from estados where entregado = 0)) or o.reinsercion = 1) " . $orderBy;
													
													//echo($mySQL);
													//exit;
													
			            							$result = readSQL($mySQL);
			            							
													$i = 0;			    							
					  				                foreach($result as $value)
					  				                {
					  				                	if($i%2==0)
					  				                		$class = "par";
					  				                	else
					  				                		$class = "impar";
		  					                		?>
		  					                		<tr>
		  					                			<td class="<?php echo $class; ?>"><?php echo $value['MARCA'] . " " . $value['nombreComercial'] ;?></td>
		  					                			<td class="<?php echo $class; ?>" align="right"><?php echo $value['imei'] ;?></td>
		  					                			<td class="<?php echo $class; ?>"><?php echo $value['nombre'] ;?></td>
			  					                		<td class="<?php echo $class; ?>" align="right"><?php echo $value['nroRemitoEmisor'] ;?></td>
                                                        <td class="<?php echo $class; ?>" align="right"><?php echo $value['nroRemitoReceptor'] ;?></td>
		  					                		</tr>
                						<?php
					  				                }
		                						}
		                						else
		                						{
		                						
            							?>
	            									<tr>
	            										<td colspan="6">Deposito: <b><?php echo $_POST[txtDeposito] . $_GET[deposito]; ?></b></td>
	            									</tr>
	            									<tr>
	            										<td>&nbsp;</td>
	            									</tr>
			            							<tr>
			            								<?
			            								$order1 = "1";
		            									$order2 = "3";
		            									$order3 = "5";
		            									$order4 = "7";
		            									$order5 = "9";
		            									$order6 = "11";
			            								switch($_GET[order])
				            							{
				            								case 1:
				            								{
				            									$orderBy = " ORDER BY P.codigoTango ASC";
				            									$order1 = "2";
				            									break;
				            								}
				            								case 2:
				            								{
				            									$orderBy = " ORDER BY P.codigoTango DESC";
				            									$order1 = "1";
				            									break;
				            								}
				            								case 3:
				            								{
				            									$orderBy = " ORDER BY P.descripcionInterna ASC";
				            									$order2 = "4";
				            									break;
				            								}
				            								case 4:
				            								{
				            									$orderBy = " ORDER BY P.descripcionInterna DESC";
				            									$order2 = "3";
				            									break;
				            								}
				            								case 5:
				            								{
				            									$orderBy = " ORDER BY S.imei ASC";
				            									$order3 = "6";
				            									break;
				            								}
				            								case 6:
				            								{
				            									$orderBy = " ORDER BY S.imei DESC";
				            									$order3 = "5";
				            									break;
				            								}
				            								case 7:
				            								{
				            									$orderBy = " ORDER BY S.cantidad ASC";
				            									$order4 = "8";
				            									break;
				            								}
				            								case 8:
				            								{
				            									$orderBy = " ORDER BY S.cantidad DESC";
				            									$order4 = "7";
				            									break;
				            								}
				            								case 9:
				            								{
				            									$orderBy = " ORDER BY S.fecha ASC";
				            									$order5 = "10";
				            									break;
				            								}
				            								case 10:
				            								{
				            									$orderBy = " ORDER BY S.fecha DESC";
				            									$order5 = "9";
				            									break;
				            								}
				            								case 11:
				            								{
				            									$orderBy = " ORDER BY S.nroFactura ASC";
				            									$order6 = "12";
				            									break;
				            								}
				            								case 12:
				            								{
				            									$orderBy = " ORDER BY S.nroFactura DESC";
				            									$order6 = "11";
				            									break;
				            								}
															case 13:
				            								{
				            									$orderBy = " ORDER BY S.codigoReferencia DESC";
				            									$order6 = "11";
				            									break;
				            								}
				            								default:
				            								{
				            									$order1 = "1";
				            									$order2 = "3";
				            									$order3 = "5";
				            									$order4 = "7";
				            									$order5 = "9";
				            									$order6 = "11";
				            									break;
				            								}
				            							}
			            								?>
			            								<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order1;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Codigo Tango</a></th>
			            								<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order1;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Codigo Fabricante</a></th>
			            								<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order2;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Parte</a></th>
			            								<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order3;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Imei</a></th>
                                                        <th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order13;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Codigo Referencia</a></th>
			            								<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order3;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Simlock</a></th>
			            								<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order4;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Cantidad</a></th>
			            								<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order5;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Fecha</a></th>
			            								<th class="listaHead"><a class="listaHeadLink" href="existencias.php?iOpcion=2&order=<?php echo $order6;?>&almacen=<?php echo $_POST[cboAlmacen] . $_GET[almacen];?>&deposito=<?php echo $_POST[txtDeposito] . $_GET[deposito];?>">Nro.Factura</a></th>
			            								<th class="listaHead">Tipos de Uso</th>
			            								<th class="listaHead">Modelos</th>
			            							</tr>
			            							<?php
			            							$mySQL = "SELECT P.idParte, P.descripcionInterna, P.codigoFabricante, P.codigoTango, S.imei, S.cantidad, S.fecha, S.nroFactura, S.idStock, S.simlock, S.codigoReferencia FROM partes P INNER JOIN stock S ON S.idParte = P.idParte WHERE P.codigoTango <> '111111111111' AND S.cantidad > 0 AND S.idAlmacen = " . $_POST[cboAlmacen] . $_GET[almacen] . $orderBy;
													
													//echo $mySQL."<br/>";
													//exit;
			            							$result = readSQL($mySQL);
													//echo count($result);

													$i = 0;			    							
					  				                foreach($result as $value)
					  				                {
					  				                	if($i%2==0)
					  				                		$class = "par";
					  				                	else
					  				                		$class = "impar";
			  					                	?>
			  					                	<tr>
			  					                		<td class="<?php echo $class; ?>" align="right"><?php echo $value['codigoTango']; ?></td>
			  					                		<td class="<?php echo $class; ?>" align="right"><?php echo $value['codigoFabricante']; ?></td>
			  					                		<td class="<?php echo $class; ?>"><?php echo $value['descripcionInterna']; ?></td>
			  					                		<?php
			  					                		if($value['imei'] == "0")
			  					                			$imei = "";
			  					                		else
			  					                			$imei = $value['imei'];
			  					                		?>
			  					                		<td class="<?php echo $class; ?>" align="right"><?php echo $imei ;?></td>
                                                        <td class="<?php echo $class; ?>" align="right"><?php echo $value['codigoReferencia'] ;?></td>
			  					                		<td class="<?php echo $class; ?>" align="right"><?php echo $value['simlock'] ;?></td>
			  					                		<td class="<?php echo $class; ?>" align="right"><?php echo $value['cantidad']; ?></td>
			  					                		<?php
			  					                		list($ano, $mes, $dia) =  split("-", substr($value['fecha'], 0, 10));
														$fecha = $dia . "/" . $mes . "/" . $ano;
			  					                		?>
			  					                		<td class="<?php echo $class; ?>"><?php echo $fecha; ?></td>
			  					                		<td class="<?php echo $class; ?>" align="right"><?php echo $value['nroFactura']; ?></td>
			  					                		<td class="<?php echo $class; ?>">
			  					                			<table>
			  					                				<?php
		  					                					$mySQL = "SELECT TU.nombre FROM tiposusos TU, tiposusosxstock TUS WHERE TU.idTipoUso = TUS.idTipoUso AND TUS.idStock = " . $value['idStock'];
		  					                					$result = readSQL($mySQL);					
								  				                foreach($result as $valueUso)
		  					                					{
		  					                						echo "<tr><td><li>";
		  					                						echo $valueUso[0];
		  					                						echo "</li></td></tr>";
		  					                					}
			  					                				?>
			  					                			</table>
			  					                		</td>
			  					                		<td class="<?php echo $class; ?>">
			  					                			<table>
			  					                				<?php
		  					                					$mySQL = "SELECT M.nombreComercial, MC.nombre FROM partesxmodelo PXM, modelos M, marcas MC WHERE PXM.idModelo = M.idModelo AND M.idMarca = MC.idMarca AND PXM.idParte = " . $value['idParte'];
		  					                					$result = readSQL($mySQL);					
								  				                foreach($result as $valueUso)
		  					                					{
		  					                						echo "<tr><td><li>";
		  					                						echo $valueUso[0];
		  					                						echo "</li></td></tr>";
		  					                					}
			  					                				?>
			  					                			</table>
			  					                		</td>
			  					                		<td class="<?php echo $class; ?>"><a href="cambioUsoStock.php?iStock=<?php echo $value['idStock']; ?>"><img class="imagen" src="images/modificar.png"></a></td>
			  					                	</tr>
			  					                	<?php
			  					                		$i++;
					  				                }
			                					}
					  				                ?>
					  				                <tr>
					  				                	<td colspan="6" align="center"><input type="button" name="cmdVolver" value="Volver" onClick="history.go(-1);"></td>
					  				                </tr>
					  				                <?php
		                							break;
			                					}
		                					}
		                				?>
		                			</table>
        		            	</form>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
	</body>
</html>